/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.action;

import bookstore.dao.CategoryDao;
import bookstore.dao.OrderDao;
import bookstore.dao.OrderDetailsDao;
import bookstore.iservice.ICategoryService;
import bookstore.iservice.IOrderDetailsService;
import bookstore.iservice.IOrderService;
import bookstore.model.Category;
import bookstore.model.Customer;
import bookstore.model.Good;
import bookstore.model.Order;
import bookstore.model.Orderdetails;
import bookstore.service.CategoryService;
import bookstore.service.OrderDetailsService;
import bookstore.service.OrderService;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 *
 * @author Administrator
 */
@WebServlet(name = "OrderInfoAction", urlPatterns = {"/OrderInfoAction"})
public class OrderInfoAction extends HttpServlet {

    private ICategoryService categoryService;
    private IOrderService orderService = new OrderService();
    private Order order = null;
    private IOrderDetailsService orderDetailsService = new OrderDetailsService();
    private Orderdetails orderdetail = null;

    public OrderInfoAction() {
        categoryService = new CategoryService();
        categoryService.setCategoryDao(new CategoryDao());
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=gbk");
        String method = request.getParameter("method");
        if (method.equals("manage")) {
            manage(request, response);
        } else if (method.equals("add")) {
            add(request, response);
        } else if (method.equals("willEdit")) {
            willEdit(request, response);
        } else if (method.equals("edit")) {
            edit(request, response);
        } else if (method.equals("delete")) {
            delete(request, response);
        }
    }

    protected void manage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String total = request.getParameter("total");
        String orderState = request.getParameter("orderState");
        String reciever = request.getParameter("reciever");
        String address = request.getParameter("address");
        String zipcode = request.getParameter("zipcode");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String sendWay = request.getParameter("sendWay");
        String payWay = request.getParameter("payWay");
        System.out.println(sendWay+"  "+reciever+"  "+address+"  "+zipcode+"  "+phone+"  "+email+"  ");
        HttpSession session = request.getSession();
        Customer customer =(Customer) session.getAttribute("bsCustomer");
        List<Good> goodList = (List<Good>) session.getAttribute("goodList");
        int ord_id = 0;     
        order = new Order();
        order.setCustomer(customer);
        order.setOrderState(orderState);
        order.setPayWay(payWay);
        order.setSendWay(sendWay);
        order.setTotal(Float.parseFloat(total));
        order.setAddress(address);
        order.setEmail(email);
        order.setPhone(phone);
        order.setZipcode(zipcode);
        order.setReciever(reciever);
        orderService.setOrderDao(new OrderDao());
        ord_id=orderService.addOrder(order);
        order.setId(ord_id);
         for(int i = 0;i<goodList.size();++i)
        {
            orderdetail = new Orderdetails();
            orderdetail.setOrder(order);
            orderdetail.setBook(goodList.get(i).getBook());
            orderdetail.setCount(goodList.get(i).getCount());
            orderdetail.setTotal(goodList.get(i).getTotal());
            orderDetailsService.setDetailDao(new OrderDetailsDao());
            orderDetailsService.addOrderDetails(orderdetail);
        }      
        System.out.print("success");
        session.removeAttribute("goodList");
       orderService.setOrderDao(new OrderDao());
       List<Order> orderList=orderService.findOrder(customer);
       session.setAttribute("orderList", orderList);
        RequestDispatcher rd = request.getRequestDispatcher("/book/showOrder.jsp");
        rd.forward(request, response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Category category = new Category();
        category.setName(request.getParameter("name"));
        categoryService.addCategory(category);
        request.setAttribute("msg", "添加分类成功！<a href=\"/iBook/CategoryAction?method=manage");
        RequestDispatcher rd = request.getRequestDispatcher("admin/categoryAdd.jsp?ModalId=myModal");
        rd.forward(request, response);
    }

    protected void willEdit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.findCategoryById(id);
        request.setAttribute("category", category);
        //categoryService.editCategory(category);
        RequestDispatcher rd = request.getRequestDispatcher("admin/categoryEdit.jsp");
        rd.forward(request, response);
    }

    protected void edit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.findCategoryById(id);
        category.setName(request.getParameter("name"));
        categoryService.editCategory(category);
        RequestDispatcher rd = request.getRequestDispatcher("CategoryAction?method=manage");
        rd.forward(request, response);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        categoryService.deleteCategory(id);
        RequestDispatcher rd = request.getRequestDispatcher("CategoryAction?method=manage");
        rd.forward(request, response);
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

